﻿using ASmile.Helpers;
using System;
using System.Collections.Generic;
using System.Text;

namespace ASmile
{
    /// <summary>
    /// 字典扩展对象
    /// </summary>
    public class DictExtend : Dictionary<string, string>
    {
        public DictExtend() { }

        public DictExtend(System.Collections.IEnumerable data, string keyName, string valName)
        {
            Create(this, data, keyName, valName);
        }

        public static void Create(DictExtend ret, System.Collections.IEnumerable data, string keyName, string valName)
        {
            foreach (var item in data)
            {
                var a = ReflectionHelper.GetPropertyNameAndValue(item);
                ret.SetValue($"{a[keyName]}", a[valName]);
            }
        }

        /// <summary>
        /// 获取扩展参数对象
        /// </summary>
        public TRet GetObject<TRet>() where TRet : class, new()
        {
            TRet ret = new TRet();
            ReflectionHelper.SetPropertyValue(ret, this);
            return ret;
        }
        /// <summary>
        /// 获取扩展参数对象值,赋值到 TarObj
        /// </summary>
        public void GetObject(object tarObj)
        {
            ReflectionHelper.SetPropertyValue(tarObj, this);
        }
        /// <summary>
        /// 设置对象到 扩展参数 ExpandData 赋值，允许多次执行，属性名相同覆盖
        /// </summary>
        public void SetObject(object obj)
        {
            var ret = ReflectionHelper.GetPropertyNameAndValue(obj);

            foreach (var item in ret)
            {
                if (item.Value is DateTime dt)
                {
                    SetValue(item.Key, dt);
                }
                else
                {
                    SetValue(item.Key, item.Value);
                }
            }
        }
        /// <summary>
        /// 将一个字典对象设置(copy)到该键值对中
        /// </summary>
        public void SetObject(Dictionary<string, string> obj)
        {
            if (obj == null) return;
            foreach (var item in obj)
            {
                this[item.Key] = item.Value;
            }
        }
        /// <summary>
        /// 设置扩展字典的字符串类型
        /// </summary>
        public void SetValue(string key, string val)
        {
            this[key] = val;
        }
        /// <summary>
        /// 设置扩展字典的日期类型
        /// </summary>
        public void SetValue(string key, DateTime val)
        {
            SetValue(key, $"{val:yyyy-MM-dd HH:mm:ss}");
        }
        /// <summary>
        /// 设置扩展字典的日期类型
        /// </summary>
        public void SetValue(string key, TimeSpan val)
        {
            SetValue(key, $"{val:hh\\:mm}");
        }

        //"hh\\:mm"
        /// <summary>
        /// 设置扩展字典的其它值类型
        /// </summary>
        public void SetValue(string key, object val)
        {
            SetValue(key, $"{val}");
        }
        /// <summary>
        /// 获取扩展字典值
        /// </summary>
        public string GetValue(string key)
        {
            this.TryGetValue(key, out string val);
            return val;
        }
        /// <summary>
        /// 获取扩展参数字典强类型值
        /// </summary>
        public T GetValue<T>(string key)
        {
            var str = GetValue(key);
            T val = (T)Convert.ChangeType(str, typeof(T));
            return val;
        }
    }
}
